package cn.javabb.log.action;

import cn.javabb.common.model.ResponseModel;
import cn.javabb.core.entity.sys.LogDO;
import cn.javabb.log.action.base.BaseActionMap;
import cn.javabb.log.action.base.ResetLog;
import cn.javabb.log.action.model.BizMethod;
import cn.javabb.log.action.model.LoginMethod;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2019/10/27 22:50
 */
public class UserAction extends BaseActionMap {

    public static final String ADMIN_USER_LOGIN = "admin_user_login";
    public static final String WEB_USER_LOGIN = "web_user_login";
    public static final String USER_SAVE = "user_save";
    public static final String EDIT_PWD = "edit_pwd";
    public static final String EDIT_ROLE = "edit_role";

    @Override
    public void init() {
        // 用户登录行为
        putMethod(ADMIN_USER_LOGIN, new LoginMethod("用户登录","adminLogin"));
        // 用户登录行为
        putMethod(WEB_USER_LOGIN, new LoginMethod("用户登录","webLogin"));
        // 保存用户行为
        putMethod(USER_SAVE, new BizMethod("用户管理","userSave"));
        // 修改用户密码行为
        putMethod(EDIT_PWD, new BizMethod("用户密码","editPwd"));
        // 角色分配行为
        putMethod(EDIT_ROLE, new BizMethod("角色分配","editRole"));
    }

    /**
     * 后台用户登录行为方法
     */
    public void adminLogin(ResetLog resetLog){
        LogDO logDO = resetLog.getLog();
        if (resetLog.isSuccess()){
            logDO.setMessage("后台登录成功");
        }else {
            String username = (String) resetLog.getParam("username");
            ResponseModel response = (ResponseModel) resetLog.getRetValue();
            logDO.setOperName(username);
            logDO.setMessage(String.format("后台登录失败：[%s]%s", username, response.getMsg()));
        }
    }
    /**
     * 前台用户登录行为方法
     */
    public void webLogin(ResetLog resetLog){
        LogDO logDO = resetLog.getLog();
        if (resetLog.isSuccess()){
            logDO.setMessage("前台登录成功");
        }else {
            String username = (String) resetLog.getParam("username");
            ResponseModel response = (ResponseModel) resetLog.getRetValue();
            logDO.setOperName(username);
            logDO.setMessage(String.format("前台登录失败：[%s]%s", username, response.getMsg()));
        }
    }
    /**
     * 保存用户行为方法
     */
    public void userSave(ResetLog resetLog){
        resetLog.getLog().setMessage("用户成功：${userName}");
        SaveAction.defaultMethod(resetLog);
    }
    /**
     * 修改用户密码行为方法
     */
    public void editPwd(ResetLog resetLog){
        LogDO logDO = resetLog.getLog();
        String id = String.valueOf(resetLog.getParam("userId")) ;
        if (resetLog.isSuccess()){
            logDO.setMessage("修改用户密码成功:"+id);
        }else {
            logDO.setMessage("修改用户密码失败：[]"+id);
        }
    }
}
